package cn.lg.soar.common.algorithm;


import cn.lg.soar.common.util.data.DataUtil;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * 对称加密工具
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class AESUtils {

    public final static String AES = "AES";
    public final static String ALGORITHM_NAME_ECB_PADDING = "AES/ECB/PKCS5Padding";
    private Cipher ENCRYPT;
    private Cipher DECRYPT;

    public AESUtils(byte[] secret){
        this.ENCRYPT = getCipher(Cipher.ENCRYPT_MODE, secret);
        this.DECRYPT = getCipher(Cipher.DECRYPT_MODE, secret);
    }

    public AESUtils(String secret){
        this(secret.getBytes());
    }

    /**
     * 加密
     * @param data 数据
     * @return
     */
    public byte[] encode(byte[] data){
        try {
            return ENCRYPT.doFinal(data);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new AlgorithmException(e);
        }
    }

    /**
     * 加密（base64）
     * @param data 数据
     * @return
     */
    public String encodeStr(byte[] data){
        return DataUtil.byteToUrlBase64(encode(data));
    }

    /**
     * 解密
     * @param data 数据
     * @return
     */
    public byte[] decode(byte[] data){
        try {
            return DECRYPT.doFinal(data);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new AlgorithmException(e);
        }
    }

    /**
     * 解密（base64）
     * @param data 数据
     * @return
     */
    public String decodeStr(byte[] data){
        return DataUtil.byteToUrlBase64(decode(data));
    }

    /**
     * 加密
     * @param secret 密钥
     * @param data 加密数据
     * @return
     */
    public static byte[] encode(byte[] secret, byte[] data) {
        Cipher cipher = getCipher(Cipher.ENCRYPT_MODE, secret);
        try {
            return cipher.doFinal(data);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new AlgorithmException(e);
        }
    }

    /**
     * 加密（base64）
     * @param secret 密钥
     * @param data 加密数据
     * @return 返回base64数据
     */
    public static String encodeStr(byte[] secret, byte[] data) {
        return DataUtil.byteToUrlBase64(encode(secret, data));
    }

    /**
     * 解密
     * @param secret 密钥
     * @param data 数据
     * @return
     */
    public static byte[] decode(byte[] secret, byte[] data){
        Cipher cipher = getCipher(Cipher.DECRYPT_MODE, secret);
        try {
            return cipher.doFinal(data);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new AlgorithmException(e);
        }
    }

    /**
     * 解密（base64）
     * @param secret 密钥
     * @param data 数据
     * @return
     */
    public static String decodeStr(byte[] secret, byte[] data){
        return DataUtil.byteToUrlBase64(decode(secret, data));
    }


    /**
     * 创建可复用的实例
     * @param secret 密钥
     * @return
     */
    public static AESUtils create(byte[] secret) {
        return new AESUtils(secret);
    }

    /**
     * 创建可复用的实例
     * @param secret 密钥
     * @return
     */
    public static AESUtils create(String secret) {
        return new AESUtils(secret);
    }

    public static Cipher getCipher(int mode, byte[] secret) {
        Key key = new SecretKeySpec(secret, AES);
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM_NAME_ECB_PADDING);
            cipher.init(mode, key);
            return cipher;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            throw new AlgorithmException(e);
        }
    }
}
